<template>
  <div>
    <PageTool>
      <template #left>
        <span>总记录数：20条</span>
        <template />
      </template>
      <template #right>
        <el-button
          v-if="$store.state.user.userInfo.roles.points.includes('import')"
          type="warning"
          size="small"
          @click="$router.push('/import')"
        >导入excel</el-button>
        <el-button type="danger" size="small" @click="doExport">导出excel</el-button>
        <el-button type="primary" size="small" @click="doAdd">新增员工</el-button>
      </template>
    </PageTool>
    <el-card style="margin-top: 10px;">
      <el-table :data="list" border>
        <el-table-column label="序号" type="index" />
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="头像" prop="staffPhoto">
          <template v-slot="scope">
            <!-- <img :src="scope.row.staffPhoto" alt=""> -->
            <imageHolder :src="scope.row.staffPhoto" /></template>
        </el-table-column>
        <el-table-column label="工号" prop="workNumber" />
        <el-table-column label="聘用形式">
          <template v-slot="scope">{{ formatType(scope.row.formOfEmployment) }}</template>
        </el-table-column>

        <el-table-column label="部门" prop="departmentName" />
        <el-table-column label="入职时间" prop="timeOfEntry" sortable />
        <el-table-column label="操作" width="280">
          <template v-slot="scope">
            <el-button type="text" size="small" @click="$router.push('/detail?id=' + scope.row.id)">查看</el-button>
            <el-button type="text" size="small" @click="addRole(scope.row.id)">分配角色</el-button>
            <el-button type="text" size="small" @click="doDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination
          layout="total,sizes, prev, pager, next"
          :total="total"
          :page-size="pages.size"
          :page-sizes="[2, 5, 10, 20]"
          :current-page="pages.page"
          @current-change="changePage"
          @size-change="changeSize"
        />
      </el-row>
    </el-card>
    <!-- 新增角色弹框 -->
    <el-dialog :visible.sync="showDialog">
      <employees-dialog
        @close="doClose"
        @success="success"
      />
    </el-dialog>
    <!-- 分配角色弹框 -->
    <el-dialog :visible.sync="showRole">
      <assign-role v-if="showRole" :cur-id="curId" @success="successRole" />
    </el-dialog>
  </div>
</template>

<script>
import { sendEmployees, deleteEmployee } from '@/api/employees'
import formatHire from '@/constant/employees'
import employeesDialog from './employeesDialog.vue'
import AssignRole from './assignRole.vue'
const employType = formatHire.hireType.reduce((acc, cur) => {
  acc[cur.id] = cur.value
  return acc
}, {})
export default {
  components: { employeesDialog, AssignRole },
  data() {
    return {
      list: [],
      pages: {
        page: 1,
        size: 2
      },
      total: 1,
      showDialog: false,
      showRole: false,
      curId: ''
    }
  },
  created() {
    this.getEmployees()
  },
  methods: {
    // 获取员工信息
    async getEmployees() {
      const res = await sendEmployees(this.pages)
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 聘用格式转换
    formatType(typeId) {
      // const item = formatHire.hireType.find(item => item.id === typeId)
      // return item.value
      if (typeId === 1 || typeId === 2) {
        return employType[typeId]
      } else {
        return '未知'
      }
    },
    // 改变当前页面触发函数
    changePage(page) {
      this.pages.page = page
      this.getEmployees()
    },
    // 改变页码触发函数
    changeSize(size) {
      this.pages.size = size
      this.pages.page = 1
      this.getEmployees()
    },
    // 删除员工
    doDelete(id) {
      this.$confirm('是否删除员工', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.getDelete(id)
        this.list.length === 1 ? this.pages.page-- : this.pages.page
        this.pages.page = Math.max(1, this.pages.page)
        this.getEmployees()
        this.$message.success('删除成功')
      }).catch(() => {
      })
    },
    // 删除部门函数
    async getDelete(id) {
      await deleteEmployee(id)
    },
    // 新增员工
    doAdd() {
      this.showDialog = true
    },
    // 点击取消按钮，弹窗关闭
    doClose() {
      this.showDialog = false
    },
    // 添加成功回调的函数
    success() {
      this.pages.page = Math.ceil(this.total / this.pages.size)
      this.showDialog = false
      this.getEmployees()
    },
    // 获取表格数据，并转换格式
    transData(rows) {
      const header = Object.keys(rows[0])
      const data = rows.map(item => {
        return header.map(key => {
          return item[key]
        })
      })
      return { header, data }
    },
    // 导出excel表格
    doExport() {
      import('@/vendor/Export2Excel').then(async excel => {
        const res = await sendEmployees(this.pages)
        const { header, data } = this.transData(res.data.rows)
        console.log(header, data)
        // excel表示导入的模块对象
        excel.export_json_to_excel({
          header, // 表头 必填
          data, // 具体数据 必填
          filename: '员工工资表', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    // 点击分配角色
    addRole(id) {
      this.showRole = true
      this.curId = id
    },
    // 分配角色成功后，子组件传回的函数
    successRole() {
      this.showRole = false
    }
  }
}
</script>

<style>

</style>
